﻿using LocalDbControler;
using RRQMMVVM;
using RRQMSocket;
using RRQMSocket.FileTransfer;
using System;
using System.Diagnostics;
using System.IO;
using System.Timers;
using System.Windows;

namespace ViewModel.ViewModels
{
    public class DownloadFileViewModel : ViewModelBase
    {
        public DownloadFileViewModel()
        {
            this.OpenFileCommand = new ExecuteCommand(OpenFile);
            this.StartDownloadCommand = new ExecuteCommand(StartDownload);
        }

        #region 变量

        private FileClient fileClient;
        private Timer timer;

        #endregion 变量

        #region Command

        public ExecuteCommand OpenFileCommand { get; set; }
        public ExecuteCommand StartDownloadCommand { get; set; }

        #endregion Command

        #region 属性

        public string FilePath { get; set; }

        private string fileName;

        public string FileName
        {
            get { return fileName; }
            set { fileName = value; OnPropertyChanged(); }
        }

        private string fileLength;

        public string FileLength
        {
            get { return fileLength; }
            set { fileLength = value; OnPropertyChanged(); }
        }

        private double downloadProgressValue;

        public double DownloadProgressValue
        {
            get { return downloadProgressValue; }
            set { downloadProgressValue = value; OnPropertyChanged(); }
        }

        private string message;

        public string Message
        {
            get { return message; }
            set { message = value; OnPropertyChanged(); }
        }

        private string transferSpeed;

        public string TransferSpeed
        {
            get { return transferSpeed; }
            set { transferSpeed = value; OnPropertyChanged(); }
        }

        private Visibility beginReceive;

        public Visibility BeginReceive
        {
            get { return beginReceive; }
            set { beginReceive = value; OnPropertyChanged(); }
        }

        private Visibility receiveSuccess;

        public Visibility ReceiveSuccess
        {
            get { return receiveSuccess; }
            set { receiveSuccess = value; OnPropertyChanged(); }
        }

        private bool isSucceeded;

        public bool IsSucceeded
        {
            get { return isSucceeded; }
            set
            {
                isSucceeded = value;
                if (isSucceeded)
                {
                    BeginReceive = Visibility.Hidden;
                    ReceiveSuccess = Visibility.Visible;
                }
                else
                {
                    this.ReceiveButtonEnable = true;
                    BeginReceive = Visibility.Visible;
                    ReceiveSuccess = Visibility.Hidden;
                }
            }
        }

        private bool receiveButtonEnable;

        public bool ReceiveButtonEnable
        {
            get { return receiveButtonEnable; }
            set { receiveButtonEnable = value; OnPropertyChanged(); }
        }

        #endregion 属性



        #region 绑定方法

        private void OpenFile()
        {
            if (File.Exists(this.FilePath))
            {
                Process.Start(this.FilePath);
            }
        }

        private void StartDownload()
        {
            this.fileClient = new FileClient();
            if (timer != null)
            {
                timer.Stop();
                timer.Dispose();
            }
            ConnectSetting connectSetting = new ConnectSetting();
            //connectSetting.TargetIP = AppConfig.ServiceIP;
            //connectSetting.TargetPort = AppConfig.ServicePort;

            try
            {
                this.fileClient.Connect(connectSetting);
                this.fileClient.DownloadFileFinshed += FileClient_DownloadFileFinshed; ;
                FileUrl url = new FileUrl(this.FilePath);

                this.Message = "正在下载";
                try
                {
                    this.fileClient.DownloadFile(url);
                    this.ReceiveButtonEnable = false;
                    timer = new Timer(1000);
                    timer.Elapsed += Timer_Elapsed;
                    timer.Start();
                }
                catch (Exception e)
                {
                    this.Message = e.Message;
                }
            }
            catch (Exception e)
            {
                this.Message = e.Message;
            }
        }

        private void FileClient_DownloadFileFinshed(object sender, FileFinishedArgs e)
        {
            this.Message = "下载完成";
            this.IsSucceeded = true;
            this.DownloadProgressValue = 1;

            this.fileClient.Dispose();
            this.TransferSpeed = string.Empty;
            this.timer.Stop();
            this.timer.Dispose();
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.DownloadProgressValue = this.fileClient.DownloadProgressValue;
            this.TransferSpeed = string.Format("{0}Kb/s", this.fileClient.DownloadSpeed.ToString("0.00"));
        }

        #endregion 绑定方法
    }
}